from rest_framework import serializers

from .models import *
# from .models import ProImg,Module,ProType,Project,Router,RouterMeta


# 模块图片
class ProImgSerializer(serializers.ModelSerializer):
    """
    模块图片序列化
    """
    created_date = serializers.DateTimeField(required=False, format='%Y-%m-%d %H:%M:%S')
    last_edited_date = serializers.DateTimeField(required=False, format='%Y-%m-%d %H:%M:%S')
    created_by_name = serializers.CharField(source='created_by.realname',read_only=True)
    last_edited_by_name = serializers.CharField(source='last_edited_by.realname',read_only=True)
    class Meta:
        model = ProImg
        fields = "__all__"
class ProImgOptionSerializer(serializers.ModelSerializer):
    """
    模块图片下拉选序列化
    """
    value = serializers.IntegerField(source='id')
    label = serializers.CharField(source='pro_img')
    class Meta:
        model = ProImg
        fields = ['value', 'label']

# 菜单列表
class ModuleSerializer2(serializers.ModelSerializer):
    """
    菜单列表子模块序列化器【先于ModuleSerializer】
    """
    class Meta:
        model = Module
        fields = ['id', 'name', 'url','grade']
class ModuleSerializer(serializers.ModelSerializer):
    """
    菜单列表模块序列化器
    """
    sub_menu = ModuleSerializer2(many=True)
    class Meta:
        model = Module
        fields = ['id', 'name', 'url', 'sub_menu','grade']
class ModuleOptionSerializer(serializers.ModelSerializer):
    """
    菜单列表用户可操作菜单下拉选
    """
    value = serializers.IntegerField(source='id')
    label = serializers.CharField(source='name')
    class Meta:
        model = Module
        fields = ['value', 'label','parent_id']

# 应用模块类型
class ProTypeSerializer(serializers.ModelSerializer):
    """
    应用模块类型序列化
    """
    created_date = serializers.DateTimeField(required=False, format='%Y-%m-%d %H:%M:%S')
    last_edited_date = serializers.DateTimeField(required=False, format='%Y-%m-%d %H:%M:%S')
    created_by_name = serializers.CharField(source='created_by.realname',read_only=True)
    last_edited_by_name = serializers.CharField(source='last_edited_by.realname',read_only=True)
    class Meta:
        model = ProType
        fields = "__all__"
class ProTypeOptionSerializer(serializers.ModelSerializer):
    """
    应用模块类型下拉选序列化
    """
    value = serializers.IntegerField(source='id')
    label = serializers.CharField(source='name')
    class Meta:
        model = ProType
        fields = ['value', 'label']

#应用模块序列化器
class ProjectSerializer(serializers.ModelSerializer):
    """
    应用模块序列化器
    """
    created_date = serializers.DateTimeField(required=False, format='%Y-%m-%d %H:%M:%S')
    last_edited_date = serializers.DateTimeField(required=False, format='%Y-%m-%d %H:%M:%S')
    created_by_name = serializers.CharField(source='created_by.realname',read_only=True)
    last_edited_by_name = serializers.CharField(source='last_edited_by.realname',read_only=True)
    logo = serializers.ImageField(source='pro_logo.pro_img',read_only=True)
    img = serializers.ImageField(source='pro_imgs.pro_img',read_only=True)
    class Meta:
        model = Project
        fields = "__all__"
class ProjectOptionSerializer(serializers.ModelSerializer):
    """ 应用模块下拉选"""
    value = serializers.IntegerField(source='id')
    label = serializers.CharField(source='name')
    class Meta:
        model = Project
        fields = ['value', 'label']
